package org.example.controller;

import org.example.annotation.TokenRequired;
import org.example.entity.AddressEntity;
import org.example.entity.UserEntity;
import org.example.service.IAddressService;
import org.example.util.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hanjiafeng
 * @date 2021/4/20 14:55
 * @desc todo
 */
@Controller
@RequestMapping("address")
public class AddressController {

    private final IAddressService addressService;

    public AddressController(IAddressService addressService) {
        this.addressService = addressService;
    }

    @TokenRequired
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<AddressEntity>> list(UserEntity userEntity) {
        ResponseEntity<List<AddressEntity>> responseEntity;
        List<AddressEntity> addressEntityList = addressService.list(userEntity.getUid());
        responseEntity = ResponseEntity.success("查询地址成功");
        responseEntity.setData(addressEntityList);
        return responseEntity;
    }

    @TokenRequired
    @PostMapping("save")
    @ResponseBody
    public ResponseEntity<Void> save(AddressEntity addressEntity, UserEntity userEntity) {
        addressEntity.setUid(userEntity.getUid());
        addressService.save(addressEntity);
        return ResponseEntity.success("添加收获地址成功");
    }
}
